package personal.xjl._23java1studentdemo.filter;

import jakarta.servlet.*;
import jakarta.servlet.annotation.WebFilter;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import org.springframework.stereotype.Controller;

import java.io.IOException;

//@Controller
@WebFilter("/")
public class LoginFilter implements Filter {


    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
            HttpServletRequest request = (HttpServletRequest) servletRequest;
            HttpServletResponse response = (HttpServletResponse) servletResponse;
            HttpSession session = request.getSession();

            String path = request.getRequestURI();
            if(path.contains("login")) {
                filterChain.doFilter(request, response);//过滤器放行

            }else if(session.getAttribute("username")!=null){
                filterChain.doFilter(request, response);
            }else{
                response.sendRedirect("/login");
            }

    }
}
